<?php
/**
 * 省市区模块
 * @author ezhu <ezhu@jipukeji.com>
 */

namespace Shop\Model;
use Think\Model;

class AreaModel extends Model {
    protected $tableName = 'area';
    
    /**
     * 省市区字符串
     */
    public function strArea($ids=array(),$field='title'){
        $map = array();
        $str = '';
        if(empty($ids)){
            return $str;
        }
        $map['id'] = array('in',$ids);
        $data = $this->where($map)->order('`pid` ASC')->select();
        if(!empty($data)){
            foreach ($data as $key=>$val){
                $str .= '&nbsp'.$val[$field];
            }
            $str = trim($str,'&nbsp');
        }
        return $str;
    }
    
    
    /**
     * 运费模板获取区域地址
     * @param 其他规则已选择地区省市ids $hasIds
     * @param 当前编辑运费规则地区已选择省市ids  $curIds
     * @param $limit 是否限制它的地区
     * @return array
     */
    public function areaZone($hasIds=array(),$curIds=array() , $limit = '' ){
        $zMap['status'] = 1; 
        $zone = M('AreaZone')->where($zMap)->field('id,name')->order('`sort` DESC')->select();
        $zids = array_column($zone,'id');
        // 查询省市
        $map['deep'] = array('elt',2);
        if($limit) $map['id'] = array( 'in' , $limit );
        $list = $this->where($map)->select();
        $deep = arrKeyChange($list,'deep' ,false);
        $psd  = array_unique(array_column($deep[2],'pid'));
        if($limit){
            $psd = implode(',' , $psd);
            $map['id'] = array( 'in' , $limit.',' .$psd );
            $list = $this->where($map)->select();
        } 
        if(!empty($hasIds) || !empty($curIds)){
            foreach ($list as $k=>$v){
                if(in_array($v['id'],$hasIds)){
                    $list[$k]['disabled'] = 'disabled';
                }else{
                    $list[$k]['disabled'] = '';
                }
                
                if(in_array($v['id'],$curIds)){
                    $list[$k]['checked'] = 'checked';
                }else{
                    $list[$k]['checked'] = '';
                }
            }
        }
        $list = list_to_tree($list,'id','pid','child');
        $list = arrKeyChange($list,'zone_id',false);
        foreach ($zone as $key=>$val){
            $zone[$key]['province'] = $list[$val['id']];
        }
        if($limit){
            foreach($zone as $k => $v){
                if(empty($zone[$k]['province'])){
                    unset($zone[$k]);
                }
            }
        } 
        foreach($zone as $k => $v){
            foreach($zone[$k]['province'] as $kk => $vv){
                $temp_check = arrKeyChange($vv['child'],'checked' ,false) ;
                $zone[$k]['province'][$kk]['count'] = count($temp_check['checked']);
                if(count($temp_check) == 1 && $temp_check['checked'])$zone[$k]['province'][$kk]['checked'] =  'checked';
                if($zone[$k]['province'][$kk]['count'] == 0)unset($zone[$k]['province'][$kk]['count']);
                $temp_check = arrKeyChange($vv['child'],'disabled' ,false) ;
                $zone[$k]['province'][$kk]['count'] = count($temp_check['disabled']);
                if(count($temp_check) == 1 && $temp_check['disabled'])$zone[$k]['province'][$kk]['disabled'] =  'disabled';
                if($zone[$k]['province'][$kk]['count'] == 0)unset($zone[$k]['province'][$kk]['count']);
            }
        }
        return $zone;
    }
    /**
     * 区域价格 区级地区列表 
     * @param 其他规则已选择地区省市ids $hasIds
     * @param 当前编辑运费规则地区已选择省市ids  $curIds
     * @param $limit 是否限制它的地区
     * @return array
     */
    public function myZone($hasIds=array(),$curIds=array() , $limit = '' ){
        $zMap['status'] = 1;
        $zone = M('AreaZone')->where($zMap)->field('id,name')->order('`sort` DESC')->select();
        $zids = array_column($zone,'id');
        // // 查询省市区
        $map['deep'] = array('elt',3);
        if($limit) $map['id'] = array( 'in' , $limit );
        $list = $this->where($map)->select();
        $deep = arrKeyChange($list,'deep' ,false);
        $psd2  = array_unique(array_column($deep[2],'pid'));
        $psd3  = array_unique(array_column($deep[3],'pid'));
        if($limit){
            $psd4 = array();
            $last = array();
            $wherepsd3['id'] = array('in' ,$psd3);
            if($psd3){
                $psd4  = M('Area')->where($wherepsd3)->getField('pid' ,true);
            }
            $limit = explode(',' , $limit);
            $last  = array_merge($last , $limit);
            if($psd2)$last  = array_merge($last , $psd2);
            if($psd3)$last  = array_merge($last , $psd3);
            if($psd4)$last  = array_merge($last , $psd4);
            array_unique($last);
            $map['id'] = array( 'in' , $last);
            $list = $this->where($map)->select();
        } 
        if(!empty($hasIds) || !empty($curIds)){
            foreach ($list as $k=>$v){
                if(in_array($v['id'],$hasIds)){
                    $list[$k]['disabled'] = 'disabled';
                }else{
                    $list[$k]['disabled'] = '';
                }
                
                if(in_array($v['id'],$curIds)){
                    $list[$k]['checked'] = 'checked';
                }else{
                    $list[$k]['checked'] = '';
                }
            }
        }
        $list = list_to_tree($list,'id','pid','child');
        $list = arrKeyChange($list,'zone_id',false);
        foreach ($zone as $key=>$val){
            $zone[$key]['province'] = $list[$val['id']];
        }
        if($limit){
            foreach($zone as $k => $v){
                if(empty($zone[$k]['province'])){
                    unset($zone[$k]);
                }
            }
        }
        $change = function( $zones , $continue = null) use ( &$change ){
            $temp_check    = arrKeyChange($zones['child'],'checked' ,false) ;
            $zones['count'] = count($temp_check['checked']);
            if( $zones['count']  == count($zones['child']) )$zones['checked'] =  'checked';
            if( $zones['count'] == 0 )$zones['count'] = '';
            $temp_check = arrKeyChange($vv['child'],'disabled' ,false) ;
            $zones['dis_count'] = count($temp_check['disabled']);
            if($zones['dis_count'] == count($zones['child']) ) $zones['disabled'] =  'disabled';
           return $zones;
        }; 
        foreach($zone as $k => $v){
            foreach ($v['province'] as $kk => $vv) {
                $zone[$k]['province'][$kk] = $change($vv);
            }
        }
        return $zone;
    }
    
    
}